/*************************************************
 * File name: sport_data.c
 * Description: 链表与文件读写实现
 *************************************************/
#include "sport_data.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

ProjectList Project_LoadFromFile(const char *file_name)
{
    FILE *fp = fopen(file_name, "r");
    if (!fp) return NULL;
    ProjectList head = NULL;
    ProjectNode tmp;
    while (fscanf(fp, "%s %s %s %lf",
                  tmp.id, tmp.name, tmp.category, &tmp.score) == 4) {
        ProjectNode *node = (ProjectNode *)malloc(sizeof(ProjectNode));
        *node = tmp;
        node->next = NULL;
        head = Project_Insert(head, node);
    }
    fclose(fp);
    return head;
}

int Project_SaveToFile(ProjectList head, const char *file_name)
{
    FILE *fp = fopen(file_name, "w");
    if (!fp) return -1;
    ProjectNode *p = head;
    while (p) {
        fprintf(fp, "%-16s %-16s %-16s %-16.2f\n",
                p->id, p->name, p->category, p->score);
        p = p->next;
    }
    fclose(fp);
    return 0;
}

ProjectList Project_Insert(ProjectList head, ProjectNode *node)
{
    if (!head) return node;
    ProjectNode *cur = head;
    while (cur->next) cur = cur->next;
    cur->next = node;
    return head;
}

ProjectList Project_Delete(ProjectList head, ProjectNode *node)
{
    if (!head) return NULL;
    if (head == node) {
        ProjectNode *nxt = head->next;
        free(head);
        return nxt;
    }
    ProjectNode *cur = head;
    while (cur->next) {
        if (cur->next == node) {
            cur->next = node->next;
            free(node);
            break;
        }
        cur = cur->next;
    }
    return head;
}

ProjectNode* Project_FindById(ProjectList head, const char *id)
{
    ProjectNode *p = head;
    while (p) {
        if (strcmp(p->id, id) == 0) return p;
        p = p->next;
    }
    return NULL;
}

int Project_Count(ProjectList head)
{
    int cnt = 0;
    ProjectNode *p = head;
    while (p) { ++cnt; p = p->next; }
    return cnt;
}

PlayerList Player_LoadFromFile(const char *file_name, int *p_serial)
{
    FILE *fp = fopen(file_name, "r");
    if (!fp) return NULL;
    fscanf(fp, "%d", p_serial);
    PlayerList head = NULL;
    PlayerNode tmp;
    while (fscanf(fp, "%s %s %s %s %s %s %s %lf %d",
                  tmp.serial, tmp.dept_id, tmp.dept_name,
                  tmp.proj_id, tmp.proj_name, tmp.proj_cate,
                  tmp.status, &tmp.grade, &tmp.rank) == 9) {
        PlayerNode *node = (PlayerNode *)malloc(sizeof(PlayerNode));
        *node = tmp;
        node->next = NULL;
        head = Player_Insert(head, node);
    }
    fclose(fp);
    return head;
}

int Player_SaveToFile(PlayerList head, int serial, const char *file_name)
{
    FILE *fp = fopen(file_name, "w");
    if (!fp) return -1;
    fprintf(fp, "%d\n", serial);
    PlayerNode *p = head;
    while (p) {
        fprintf(fp, "%-16s %-16s %-16s %-16s %-16s %-16s %-16s %-16.2f %-16d\n",
                p->serial, p->dept_id, p->dept_name,
                p->proj_id, p->proj_name, p->proj_cate,
                p->status, p->grade, p->rank);
        p = p->next;
    }
    fclose(fp);
    return 0;
}

PlayerList Player_Insert(PlayerList head, PlayerNode *node)
{
    if (!head) return node;
    PlayerNode *cur = head;
    while (cur->next) cur = cur->next;
    cur->next = node;
    return head;
}

PlayerList Player_Delete(PlayerList head, PlayerNode *node)
{
    if (!head) return NULL;
    if (head == node) {
        PlayerNode *nxt = head->next;
        free(head);
        return nxt;
    }
    PlayerNode *cur = head;
    while (cur->next) {
        if (cur->next == node) {
            cur->next = node->next;
            free(node);
            break;
        }
        cur = cur->next;
    }
    return head;
}

PlayerNode* Player_FindBySerial(PlayerList head, const char *serial)
{
    PlayerNode *p = head;
    while (p) {
        if (strcmp(p->serial, serial) == 0) return p;
        p = p->next;
    }
    return NULL;
}

int Player_Count(PlayerList head)
{
    int cnt = 0;
    PlayerNode *p = head;
    while (p) { ++cnt; p = p->next; }
    return cnt;
}